//1.导入express包
var express = require('express')
//2.创建router路由对象
var router = express.Router()
//3.编写路由规则
//导入数据库
const { getsqlall, getone, getfun } = require('../db/index')

/* 获取所有代办 */
router.get('/', async (req, res) => {
  // 解析请求参数
  // 编写sql语句
  let sql = `select * from vue_todos`
  //console.log(sql);
  // 执行sql语句,返回结果
  try {
    const data = await getsqlall(sql)
    res.send({
      code: 0,
      message: '获取所有待办成功',
      result: data,
    })
  } catch (error) {
    res.send({
      code: 100103,
      message: '获取所有待办失败',
      result: '',
    })
  }
})

/* 获取单个代办 */
router.get('/:id', async (req, res) => {
  // 解析请求参数
  const { id } = req.params
  //对参数的格式进行校验
  const reg = /^\d+$/ /* 数字出现一次或者多次,并且以数字开头和结尾 */
  if (reg.test(id) == false) {
    res.send({
      code: 100104,
      message: 'id必须为数字',
      result: '',
    })
    return
  }
  // 编写sql语句
  let sql = `select id,content,completed from vue_todos where id=${id}`
  console.log(sql)
  // 执行sql语句,返回结果

  try {
    const data = await getsqlall(sql)
    if (data != null) {
      res.send({
        code: 0,
        message: '获取单个待办成功',
        result: data,
      })
    } else {
      res.send({
        code: 100102,
        message: 'id对应的数据不存在',
        result: data,
      })
    }
  } catch (error) {
    res.send({
      code: 100104,
      message: 'id必须为数字',
      result: '',
    })
  }
})

/* 新增代办-post /todos*/
router.post('/', async (req, res) => {
  // 解析请求参数
  const { content } = req.body
  //对请求的参数的格式进行校验(不能为空或null)
  if (!content) {
    //如果content是空或者是null,取反的结果就是true,就可以执行里面的代码
    res.send({
      code: 100106,
      message: 'content格式错误',
      result: '',
    })
    return
  }
  // 编写sql语句
  let sql = `insert into vue_todos (content) values ('${content}')`
  //从插入的结果中解构出insertId
  try {
    var { insertId } = await getfun(sql)
  } catch (error) {
    res.send({
      code: 100105,
      message: '添加待办失败',
      result: debug ? error : '',
    })
    return
  }
  //重新编写SQL语句
  sql = `select id,content,completed from vue_todos where id=${insertId}`
  // 执行sql语句,返回结果
  try {
    const data = await getone(sql)
    res.send({
      code: 0,
      message: '添加待办成功',
      result: data,
    })
  } catch (error) {
    res.send({
      code: 100105,
      message: '添加待办失败',
      result: debug ? error : '',
    })
  }
})

/* 修改代办-put /todos/1 */
router.put('/:id', async (req, res) => {
  // 解析请求参数
  const { content, completed } = req.body
  const { id } = req.params
  // 编写sql语句
  let sql = `update  vue_todos set content ='${content}',completed ='${completed}' where id = ${id}`
  //从插入的结果中解构出insertId
  await getfun(sql)
  res.send({
    code: 0,
    message: '修改待办成功',
    result: {
      id,
      content,
      completed,
    },
  })
})

/* 删除代办 */
router.delete('/:id', async (req, res) => {
  // 解析请求参数
  const { id } = req.params
  // 编写sql语句
  let sql = `delete from  vue_todos where id = ${id}`
  //从插入的结果中解构出insertId
  await getfun(sql)
  res.send({
    code: 0,
    message: '删除成功',
    result: '',
  })
})

/**
 * 删除指定项
 * Delete /todos?completed=1
 */
router.delete('/', async (req, res) => {
  // 一,解析queryString  请求参数
  const { completed } = req.query
  // 二,操作数据库
  let sql = `delete from vue_todos where completed = ${completed}`
  await getfun(sql)
  res.send({
    code: 0,
    message: '删除成功',
    result: '',
  })
})

//4,导出路由对象

module.exports = router
